package com.cn.lxz.android.aws;

import android.content.Context;
import android.util.Log;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;

import java.io.File;

public class Upload {

    public String TAG= "";
    public void aaa(Context context){
        // 设置AWS凭证（这里只是示例，实际应该更安全地获取和使用凭证）
        AWSCredentials credentials = new BasicAWSCredentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");
        AmazonS3Client s3Client = new AmazonS3Client(credentials, new ClientConfiguration());
        Region region = Region.getRegion(Regions.US_EAST_1);
        s3Client.setRegion(region);
        TransferUtility transferUtility = TransferUtility.builder()
                .s3Client(s3Client)
                .context(context)
                .build();

        // 要上传的文件路径
        File fileToUpload = new File("PATH_TO_YOUR_FILE");
        TransferObserver observer = transferUtility.upload("YOUR_BUCKET_NAME", "OBJECT_KEY_IN_BUCKET", fileToUpload);



        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                Log.d(TAG, "Transfer state changed: " + state);
                if (state == TransferState.COMPLETED) {
                    Log.d(TAG, "Upload completed successfully.");
                } else if (state == TransferState.FAILED) {
                    Log.e(TAG, "Upload failed.");
                }
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                int progress = (int) ((bytesCurrent * 100L) / bytesTotal);
                Log.d(TAG, "Upload progress: " + progress + "%");
            }

            @Override
            public void onError(int id, Exception ex) {
                Log.e(TAG, "Error during upload: ", ex);
            }
        });
    }
}
